package com.xiaoyu.client.ui.activity.help;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.xiaoyu.client.R;
import com.xiaoyu.client.adapter.seek.ThankedAdapter;
import com.xiaoyu.client.model.seek.SeekReplyParam;
import com.xiaoyu.client.model.seek.SeekThankPersonBean;
import com.xiaoyu.client.network.NetworkManager;
import com.xiaoyu.commonlib.network.CommonHttpResponser;
import com.xiaoyu.commonlib.ui.base.BaseActivity;
import com.xiaoyu.commonlib.ui.widget.ContainsEmojiEditText;
import com.xiaoyu.commonlib.utils.StatusBarUtil;
import com.xiaoyu.commonlib.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 结束求助问题
 */
public class SeekOverActivity extends AppCompatActivity implements View.OnClickListener, RadioGroup.OnCheckedChangeListener {
    @BindView(R.id.activity_seek_over_title_back_btn)
    ImageView backBtn;//返回
    @BindView(R.id.activity_seek_over_title_commit_btn)
    TextView commitBtn;//提交
    @BindView(R.id.activity_seek_over_solution_layout)
    RadioGroup mSolutionGroup;//解决方式
    @BindView(R.id.activity_seek_over_thanked_person_list_layout)
    GridView mThankedListContainer;//被感谢人的列表容器
    @BindView(R.id.activity_seek_over_thanks_world_edit)
    ContainsEmojiEditText mThanksEdit;//感谢话语文本编辑框
    @BindView(R.id.activity_seek_over_thanks_world_count)
    TextView mWorldNumTxt;//感谢话的字数
    @BindView(R.id.activity_seek_over_linear)
    LinearLayout mOverLinear;

    private ThankedAdapter mAdapter;
    private List<SeekReplyParam> mList = new ArrayList<>();
    private List<String> mUserList = new ArrayList<>();
    private String seekId;//求助id
    private int solution = 1;//完成方式： 看帖解决1，自己解决2，未解决0

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_seek_over);
        if (getSupportActionBar() != null) {
            getSupportActionBar().hide();
        }
        StatusBarUtil.StatusBarLightMode(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setStatusBarColor(getResources().getColor(R.color.colorWhite));
        }

        parseIntent();
        ButterKnife.bind(this);
        initView();
        getThankInfo();
    }

    //接收从其他页面传过来的值
    private void parseIntent() {
        Intent intent = getIntent();
        if (intent != null) {
            seekId = intent.getStringExtra(HelpDetailActivity.SEEK_ID);
        }
        ToastUtil.logResult("接收的求助id", seekId);
    }

    private void initView() {
        mUserList.clear();
        mAdapter = new ThankedAdapter(this, mList);
        mAdapter.setSelectList(mUserList);
        mThankedListContainer.setAdapter(mAdapter);

        //完成方式： 看帖解决1，自己解决2，未解决0
        ((RadioButton) mSolutionGroup.getChildAt(0)).setChecked(true);
        solution = 1;

        backBtn.setOnClickListener(this);
        commitBtn.setOnClickListener(this);
        mSolutionGroup.setOnCheckedChangeListener(this);

        mThanksEdit.addTextChangedListener(onTextWatcher);
        mThankedListContainer.setOnItemClickListener(onItemClickListener);
    }

    @Override
    public void onClick(View v) {
        if (v == backBtn) {//返回按钮
            onBackPressed();
        } else if (v == commitBtn) {//提交按钮
            toOverSeek();
        }
    }


    /**
     * 感谢人列表item点击监听
     */
    private AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if (mUserList.indexOf(mList.get(position).getUserid()) == -1) {
                mUserList.add(mList.get(position).getUserid());
            } else {
                mUserList.remove(mList.get(position).getUserid());
            }
            mAdapter.setSelectList(mUserList);
        }
    };


    /**
     * 感谢话语文本编辑框监听
     */
    private TextWatcher onTextWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            String count = s.length() + "/200";
            mWorldNumTxt.setText(count);
        }
    };


    /**
     * 获取被感谢人的列表
     */
    private void getThankInfo() {
        mList.clear();
        NetworkManager.getThankedPersonList(seekId, new CommonHttpResponser() {
            @Override
            public void onSuccess(String result) {
                parsePersonInfoSuc(result);
            }

            @Override
            public void onFailed(String errorCode, String errorDiscription) {
            }
        });
    }

    private void parsePersonInfoSuc(String result) {
        SeekThankPersonBean bean = new Gson().fromJson(result, SeekThankPersonBean.class);
        if (bean.getData() != null || bean.getData().size() != 0) {
            mList.addAll(bean.getData());
        }
        mAdapter.notifyDataSetChanged();
    }

    /**
     * 结束问题
     */
    private void toOverSeek() {
        if (solution == 1 && mUserList.size() == 0) {//完成方式： 看帖解决1，自己解决2，未解决0
            ToastUtil.showToast(this, "请选择要感谢的人！");
            return;
        }
        NetworkManager.overSeek(mUserList, mThanksEdit.getText().toString(), seekId, solution, new CommonHttpResponser() {
            @Override
            public void onSuccess(String result) {
                ToastUtil.showToast(SeekOverActivity.this, "提交成功");
                finish();
            }

            @Override
            public void onFailed(String errorCode, String errorDiscription) {
                Log.i("jx", "onFailed:失败原因===== " + errorCode + errorDiscription);
            }
        });
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.activity_seek_over_solution_look_btn:
                mOverLinear.setVisibility(View.VISIBLE);
                solution = 1;
                break;
            case R.id.activity_seek_over_solution_self_btn:
                mOverLinear.setVisibility(View.GONE);
                solution = 2;
                break;
            case R.id.activity_seek_over_unsolved_btn:
                mOverLinear.setVisibility(View.GONE);
                solution = 0;
                break;
        }
    }
}
